//
//  BQShopPayController.m
//  BeeQuick
//
//  Created by student on 2017/7/15.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQShopPayController.h"
#import "BQShopModelArrayManager.h"
#import "BQShopPayCell.h"
#import "BQPayModel.h"
#import "BQPayHeaderTitle.h"
#import "BQShopBottomView.h"
#import <Masonry.h>
#import "BQMineReceiveController.h"
@interface BQShopPayController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *payModelArray;

@property(nonatomic,strong)NSArray *headerTitleModleArray;

@property(nonatomic,weak)UITableView *tableView;

@end

@implementation BQShopPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"结算付款";
    [self addTableView];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self loadData];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self registerCell];
    [self addBottomView];
}

- (void)addTableView{
    
    UITableView *tableView = [[UITableView alloc]init];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.leading.equalTo(self.view);
    }];
    
}
- (void)addBottomView{
    
    BQShopBottomView *bottomView = [[BQShopBottomView alloc]init];
    bottomView.sureBlock = ^(){
        NSLog(@"确认付款");
    };
    bottomView.sure = @"确认付款";
    NSString *price = [[BQShopModelArrayManager manager] showSumPrice];
    bottomView.prcieLabelText = [NSString stringWithFormat:@"实付金额: %@",price];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.equalTo(self.view);
        make.height.offset(60);
        make.bottom.equalTo(self.tableView.mas_bottom);
    }];

    
}

- (void)loadData{
    
    NSString *fileName = [[NSBundle mainBundle]pathForResource:@"BQShopPay.plist" ofType:nil];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:fileName];
    NSMutableArray *mtabArr = [NSMutableArray array];
    for (NSDictionary *dict in tempArray) {
        BQPayModel *model = [[BQPayModel alloc]init];
        [model setValuesForKeysWithDictionary:dict];
        [mtabArr addObject:model];
    }
    self.payModelArray = mtabArr.copy;
    NSString *headerFileName = [[NSBundle mainBundle]pathForResource:@"BQShopHeader.plist" ofType:nil];
    NSArray *headerArr = [NSArray arrayWithContentsOfFile:headerFileName];
     NSMutableArray *Arr = [NSMutableArray array];
    for (NSDictionary *dic in headerArr) {
        BQPayHeaderTitle *model = [[BQPayHeaderTitle alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [Arr addObject:model];
    }
    self.headerTitleModleArray = Arr.copy;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 5;
    }else if (section == 3){
        return 2;
    }
    
    return 2 + [BQShopModelArrayManager manager].modelArray.count - 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identfier;
    if (indexPath.section == 0) {
        identfier = @"BQShopPayCell";
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            identfier = @"BQShopPayHeader";
        }else{
            identfier = @"BQShopShowPay";
        }
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            identfier = @"BQShopPayHeader";
        }else{
            identfier = @"BQShopShowPrice";
        }
    }else {
        if (indexPath.row == 0) {
            identfier = @"BQShopPayHeader";
        }else if (indexPath.row == ([BQShopModelArrayManager manager].modelArray.count - 3 + 1)){
            identfier = @"BQShopPayMoney";
        }else{
            identfier = @"BQShopGoods";
        }
       }
    BQShopPayCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier forIndexPath:indexPath];
    if (indexPath.section == 1) {
        if ( indexPath.row == 0) {
            BQPayHeaderTitle *headerModel = self.headerTitleModleArray[indexPath.section - 1];
            cell.headerModel = headerModel;
        }else{
            BQPayModel *payModel = self.payModelArray[indexPath.row - 1];
            cell.payModel = payModel;
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            BQPayHeaderTitle *headerModel = self.headerTitleModleArray[indexPath.section - 1];
            cell.headerModel = headerModel;
        }
        if (indexPath.row > 0 && indexPath.row < ([BQShopModelArrayManager manager].modelArray.count - 3) + 1) {
            BQProductModel *model = [BQShopModelArrayManager manager].modelArray[indexPath.row + 2];
            cell.productModel = model;
        }
    }
    
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            BQPayHeaderTitle *headerModel = self.headerTitleModleArray[indexPath.section - 1];
            cell.headerModel = headerModel;
        }else{
            BQProductModel *model = [BQShopModelArrayManager manager].modelArray[indexPath.row +2];
            cell.productModel = model;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 15;
}

- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopPayCell" bundle:nil] forCellReuseIdentifier:@"BQShopPayCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopShowPay" bundle:nil] forCellReuseIdentifier:@"BQShopShowPay"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopGoods" bundle:nil] forCellReuseIdentifier:@"BQShopGoods"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopShowPrice" bundle:nil] forCellReuseIdentifier:@"BQShopShowPrice"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopPayHeader" bundle:nil] forCellReuseIdentifier:@"BQShopPayHeader"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BQShopPayMoney" bundle:nil] forCellReuseIdentifier:@"BQShopPayMoney"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 3 && indexPath.row == 1) {
        return 127;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section==0 && indexPath.row == 0) {
        BQMineReceiveController *receiveVC = [[BQMineReceiveController alloc]init];
        [self.navigationController pushViewController:receiveVC animated:NO];
    }
}

@end
